  <template>
    <a-card :bordered="false">
    <div class="container">
      <div class="one-third-width">
        <!-- 下拉框，选择工作站 -->
        <label for="customer-select" class="select-label">工作站选择 : </label>
        <select v-model="selectedStations" id="station-select" class="select-box">
          <option value="" disabled selected>请选择工作站</option>
          <option v-for="(station, index) in stations" :key="index" :value="station">
            {{station}}
          </option>
        </select>
        <div style="text-align: center; font-weight: 700; font-size: 20px;">{{shelfCode}}{{isTwoSided}}</div>
        <div class="one-third-width1">
            <Lattice :row-num="rowNum" :col-num="colNum" :data="dataList">
            <template slot-scope="obj">
                <div class="grid-item" :style="{backgroundColor: obj.data.color}" @click="getCompartmentUtilization(obj)">
                <div class="grid-item-content">{{obj.data.compartmentCode}}</div>
                <div class="compartment-utilization" v-if="obj.data.id">{{selectedCompartmentUtilization[obj.data.id]}}</div>
                </div>
            </template>
            </Lattice>
        </div>
        <div style="text-align: center; font-weight: 700; font-size: 20px;">{{shelfUtilization}}</div>
      </div>
      <div class="two-third-width">
        <div class="button-container">
          <button type="primary" class="custom-button" @click="rotate">转面</button>
          <button type="primary" class="custom-button">呼叫货架</button>
          <button type="primary" class="custom-button">放行</button>
        </div>
      </div>
      <div class="three-third-width">
        <!-- 下拉框，选择客户 -->
        <label for="customer-select" class="select-label">货主选择 : </label>
        <select v-model="selectedCustomer" id="customer-select" class="select-box" @change="handleCustomerChange">
          <option value="" disabled selected>请选择货主</option>
          <option v-for="(customer, index) in customers" :key="index" :value="customer">
            {{customer}}
          </option>
        </select>
        <a-table
          ref="table"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          class="j-table-force-nowrap"
          :customRow="rowClick"
          >
          <!-- B/F面 -->
          <template slot="isTwoSided" slot-scope="text">
            <a-tag v-if="text==0" color="blue">关闭</a-tag>
            <a-tag v-if="text==1" color="green">开启</a-tag>
          </template>
        </a-table>
      </div>
    </div>
    </a-card>
  </template>

  <script>

  import Lattice from './lattice.vue'
  import '@/assets/less/TableExpand.less'
  import { getAction, postAction } from '../../../api/manage'

  export default {
    name: 'Test',
    components: {Lattice},
    data () {
      return {
        row: {},
        selectedCompartmentUtilization: {},
        shelfCode: "",
        isTwoSided: "",
        shelfUtilization: "",
        // 单行数据点击事件
        rowClick: record=>({
          on: {
            click: ()=>{
              console.log(record, 'record');
              this.row = record;
              this.shelfCode = record.shelfCode;
              this.isTwoSided = " ("+"B"+")";
              this.rowNum = record.plie;
              this.colNum = record.colums;
              this.shelfUtilization = record.shelfUtilization + "%";
              this.getCompartment(record,  this.isTwoSided);
            }
          }
        }),
        customers: ['客户A', '客户B', '客户C'], // 客户列表
        stations: ['1', '2', '3'],// 工作站列表
        selectedCustomer: '', // 当前选中的客户
        selectedStations: '', // 当前选中的工作站
        rowNum: 3,
        colNum: 3,
        dataList: [
          {
            name: 'a1'
          }, {
            name: 'b1'
          },
          {
            name: 'c1'
          }, {
            name: 'a2'
          },
          {
            name: 'b2'
          }, {
            name: 'c2'
          }, {
            name: 'a3'
          },
          {
            name: 'b3'
          }, {
            name: 'c3'
          }
        ],
        dataSource: [],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'货架编码',
            align:"center",
            dataIndex: 'shelfCode'
          },
          {
            title:'货架名称',
            align:"center",
            dataIndex: 'shelfName'
          },
          {
            title:'货架类型编码',
            align:"center",
            dataIndex: 'shelfTypeCode'
          },
          // {
          //   title:'货架长度(mm)',
          //   align:"center",
          //   dataIndex: 'length'
          // },
          // {
          //   title:'货架宽度(mm)',
          //   align:"center",
          //   dataIndex: 'width'
          // },
          // {
          //   title:'货架高度(mm)',
          //   align:"center",
          //   dataIndex: 'high'
          // },
          // {
          //   title:'货架层数',
          //   align:"center",
          //   dataIndex: 'plie'
          // },
          // {
          //   title:'货架格数',
          //   align:"center",
          //   dataIndex: 'colums'
          // },
          // {
          //   title:'货架容积(mm³)',
          //   align:"center",
          //   dataIndex: 'volume'
          // },
          {
            title:'货架使用率(%)',
            align:"center",
            dataIndex: 'shelfUtilization',
          },
          // {
          //   title:'B/F面',
          //   align:"center",
          //   dataIndex: 'isTwoSided',
          //   scopedSlots: {customRender: 'isTwoSided'}
          // },
          // {
          //   title:'承重(g)',
          //   align:"center",
          //   dataIndex: 'loadBearing'
          // },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   align:"center",
          //   fixed:"right",
          //   width:147,
          //   scopedSlots: { customRender: 'action' }
          // }
        ],
        url: {
          list: "/basicShelf/basicShelf/list",
          getCustomers: "/basicCustomer/basicCustomer/getCustomers",//查询货主
          getShelfs: "/inOrder/inOrder/queryShelf",//查询货架
          getCompartment: "/basicCompartment/basicCompartment/getCompartment",//查询货格
          getStations: "/basicStationInfo/basicStationInfo/list",//查询工作站
        },
      }
    },
    created() {
      this.getCustomers();
      this.getStations();
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      //获取货主名称
      getCustomers(){
        let that = this;
        getAction(this.url.getCustomers).then(res => {
            console.log(res);
            that.customers = res.result;
          }).catch((error)=>{
            console.log(error);
          })
      },
      //获取工作站名称
      getStations(){
        let that = this;
        getAction(this.url.getStations).then(res => {
            const list = res.result.records.map(item=>{return item.stationName})
            that.stations = list;
          }).catch((error)=>{
            console.log(error);
          })
      },
      //选中货主后查询对应货架
      handleCustomerChange() {
        let that = this;
        console.log('选中的客户：', this.selectedCustomer);
        postAction(this.url.getShelfs, this.selectedCustomer).then(res => {
            console.log(res);
            that.dataSource = res.result;
          }).catch((error)=>{
            console.log(error);
          })
      },
      // 查询货格及使用率
      getCompartment(record, isTwoSided){
        let that = this;
        postAction(this.url.getCompartment, record).then(res => {
            // 将列表按每层显示的格数分成对应的小组
            const chunks = [];
            for (let i = 0; i < res.result.length; i += that.colNum) {
              chunks.push(res.result.slice(i, i + that.colNum));
            }
            // 对集合进行倒序排列
            const chunksList = [];
            const reversedChunks = chunks.reverse();
            for (let i = 0; i < reversedChunks.length; i ++) {
              chunksList.push(...reversedChunks[i]);
            }
            // 过滤符合条件的元素
            let filteredChunks = null;
            if(isTwoSided === " (B)"){
              this.isTwoSided = " (B)"
              filteredChunks = chunksList.filter(chunk => chunk.isTwoSided === "B");
            }else{
              this.isTwoSided = " (F)"
              filteredChunks = chunksList.filter(chunk => chunk.isTwoSided === "F");
            }

            const list = filteredChunks.map(chunk =>{
              if(chunk.compartmentUtilization == 0){
                chunk.color = "lightgreen";
              }else if (chunk.compartmentUtilization > 0 && chunk.compartmentUtilization < 50) {
                chunk.color = "lightblue";
              } else if (chunk.compartmentUtilization >= 50 && chunk.compartmentUtilization < 90) {
                chunk.color = "yellow";
              } else {
                chunk.color = "lightcoral";
              }
              return chunk;
            });
            console.log("货格数据", list);
            that.dataList = list;
          }).catch((error)=>{
            console.log(error);
            that.dataList = null;
          })
      },
      // 页面货格使用率赋值
      getCompartmentUtilization(obj) {
        this.$set(this.selectedCompartmentUtilization, obj.data.id, obj.data.compartmentUtilization+"%");
      },
      rotate(){
        //TODO 调用RMS调转货架
        if(this.isTwoSided === " (B)"){
          this.getCompartment(this.row, " (F)");
        }else{
          this.getCompartment(this.row, " (B)");
        }
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'shelfCode',text:'货架编码',dictCode:''})
        fieldList.push({type:'string',value:'shelfName',text:'货架名称',dictCode:''})
        fieldList.push({type:'string',value:'shelfTypeCode',text:'货架类型编码',dictCode:''})
        fieldList.push({type:'int',value:'length',text:'货架长度',dictCode:''})
        fieldList.push({type:'int',value:'width',text:'货架宽度',dictCode:''})
        fieldList.push({type:'int',value:'high',text:'货架高度',dictCode:''})
        fieldList.push({type:'int',value:'plie',text:'货架层数',dictCode:''})
        fieldList.push({type:'int',value:'colums',text:'货架格数',dictCode:''})
        fieldList.push({type:'int',value:'volume',text:'货架容积',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'shelfUtilization',text:'货架使用率',dictCode:''})
        fieldList.push({type:'int',value:'isTwoSided',text:'B/F面',dictCode:''})
        fieldList.push({type:'int',value:'loadBearing',text:'承重(g)',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
  </script>
  <style scoped>
  @import '~@assets/less/common.less';
  .ant-card {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    color: rgba(0, 0, 0, 0.65);
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5;
    list-style: none;
    font-feature-settings: 'tnum';
    position: relative;
    background: #fff;
    border-radius: 2px;
    transition: all 0.3s;
    height: calc(100vh + 200px);
}
  .container {
    justify-content: center; /* 设置水平居中 */
    display: flex;
    }
    .select-label {
    font-weight: bold;
    font-size: 16px;
  }

.select-box {
  /* 样式变更 */
  /* 添加自定义样式以美化下拉框外观 */
  background-color: #f2f2f2;
  border: 2px solid #000000;
  border-radius: 4px;
  padding: 8px;
  font-size: 14px;
  width: 50%;
  margin-bottom: 16px; /* 添加下边距 */
}

.one-third-width1::before {
  content: "";
  position: absolute;
  top: 0;
  left: -10px; /* 贴在最左侧边线位置 */
  width: 10px;
  height: 100%;
  background-color: #000000;
  transform: translateY(80px); /* 向下延生 10px */
}

.one-third-width1::after {
  content: "";
  position: absolute;
  top: 0;
  right: -9.5px; /* 贴在最右侧边线位置 */
  width: 10px;
  height: 100%;
  background-color: #000000;
  transform: translateY(80px); /* 向下延生 10px */
}
.one-third-width1 {
    width: 400px;
    height: 319px;
    border: 10px solid #000000;
    position: relative;
    border-collapse: collapse;
  }
  .one-third-width {
    flex: 1;
    width: 50%;
  }
  .two-third-width {
    width: 20.67%;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .three-third-width {
    flex: 1;
    width: 75%;
  }
  .grid-item {
    height: 100px;
    border: 1px solid #ccc;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .grid-item-content {
    font-weight: bold;
  }
  .compartment-utilization {
  margin-top: 30px; /* 设置上下间距 */
  font-weight: bold;
  text-align: center; /* 居中显示 */
}
.custom-button {
  width: 120px;
  height: 40px;
  margin: 20px;
  background-color: #ccc;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background-color: #0c0c0c; /* 设置按钮的背景颜色为绿色 */
  color: white; /* 设置文字颜色为白色 */
}
.custom-button:hover {
  background-color: rgb(7, 7, 7); /* 当鼠标悬停时按钮颜色变化 */
}

.custom-button:active {
  background-color: #969796; /* 当按钮被点击时的颜色 */
}

.button-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

  </style>

